package com.sc.nft.admin.controller;


import com.sc.nft.entity.dto.AdminMallActivityShareTeamPageDTO;
import com.sc.nft.entity.dto.AdminMallShareTeamOrderPageDTO;
import com.sc.nft.entity.vo.AdminMallActivityShareTeamPageVO;
import com.sc.nft.entity.vo.AdminMallShareTeamOrderPageVO;
import com.sc.nft.service.MallActivityShareTeamService;
import com.sc.nft.service.MallShareTeamOrderService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 分享购团队信息 前端控制器
 * </p>
 *
 * @author ligen
 * @since 2023-12-13
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/activity/share/team")
@Api(tags = "分享购团队模块")
public class MallActivityShareTeamController {

    private final MallActivityShareTeamService mallActivityShareTeamService;
    private final MallShareTeamOrderService mallShareTeamOrderService;

    @ApiOperation(value = "分享成团列表", notes = "分享成团列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<AdminMallActivityShareTeamPageDTO> getMallActivityShareTeamPageDTO(@RequestBody AdminMallActivityShareTeamPageVO adminMallActivityShareTeamPageVO) {
        return PageResult.success(mallActivityShareTeamService.getAdminMallActivityShareTeamPageDTO(adminMallActivityShareTeamPageVO));
    }


    @ApiOperation(value = "成团订单列表", notes = "成团订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/order/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<AdminMallShareTeamOrderPageDTO> getMallShareTeamOrderPageDTO(@RequestBody AdminMallShareTeamOrderPageVO adminMallShareTeamOrderPageVO) {
        return PageResult.success(mallShareTeamOrderService.getAdminMallShareTeamOrderPageDTO(adminMallShareTeamOrderPageVO));
    }

    @ApiOperation(value = "分享活动团队收益结算 定时任务", notes = "分享活动团队收益结算 定时任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/shareTeamIncomeSettlement", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result shareTeamIncomeSettlement() {
        mallActivityShareTeamService.getListByShareStatus().forEach(mallActivityShareTeam -> {
            try {
                mallShareTeamOrderService.shareTeamIncomeSettlement(mallActivityShareTeam);
            } catch (Exception e) {
                log.error("分享活动团队收益结算失败 异常 teamId:{}", mallActivityShareTeam.getId(), e);
            }
        });
        return Result.success();
    }
}
